package com.rfid.assistant.activity.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.rfid.DirectoryBean;
import com.rfid.assistant.R;

import java.io.File;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class FileAdapter extends BaseAdapter {

    Context mContext;
    List<DirectoryBean.FileEntity> mFileEntities;
    DirectoryBean mBean;
    public FileAdapter(Context context,DirectoryBean bean) {
        mContext = context;
        mBean = bean;
        mFileEntities = bean.getChildren();
    }

    public void changeDir(DirectoryBean bean) {
        mFileEntities = bean.getChildren();
        DirectoryBean.FileEntity entities[] = new DirectoryBean.FileEntity[mFileEntities.size()];
        mFileEntities.toArray(entities);
        Arrays.sort(entities, new Comparator<DirectoryBean.FileEntity>() {
            @Override
            public int compare(DirectoryBean.FileEntity o1, DirectoryBean.FileEntity o2) {
                if (o1.isFile()&&!o2.isFile()) {
                    return 1;
                } else if (!o1.isFile()&&o2.isFile()) {
                    return -1;
                }
                String name1 = new File(o1.getPath()).getName().toUpperCase();
                String name2 = new File(o2.getPath()).getName().toUpperCase();
                if (name1.length() >= 2 && name2.length() >= 2) {
                    return name1.substring(0,2).compareTo(name2.substring(0,2));
                }
                return name1.charAt(0) - name2.charAt(0);
            }
        });
        mFileEntities.clear();
        mFileEntities.addAll(Arrays.asList(entities));
        System.out.println("mFileEntities size = " + mFileEntities.size());
        mBean = bean;
        notifyDataSetChanged();
    }

    public DirectoryBean getCurrentDir() {
        return mBean;
    }

    @Override
    public int getCount() {
        return mFileEntities.size();
    }

    @Override
    public DirectoryBean.FileEntity getItem(int position) {
        return mFileEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_file_layout,parent,false);
        ImageView icon = view.findViewById(R.id.icon);
        TextView name = view.findViewById(R.id.name);
        TextView size = view.findViewById(R.id.size);
        DirectoryBean.FileEntity e = mFileEntities.get(position);
        if (e.isFile()) {
            icon.setImageResource(R.mipmap.file);
        } else {
            icon.setImageResource(R.mipmap.directory);
        }
        name.setText(new File(e.getPath()).getName());
        if (e.isFile()) {
            size.setVisibility(View.VISIBLE);
            size.setText(getMSize(e.getLength()));
        } else {
            size.setVisibility(View.GONE);
        }
        return view;
    }

    private String getMSize(long size) {
        if (size < 1024) {
            return size + "B";
        } else if (size < 1024*1024) {
            return String.format("%.2f",(double)size/1024) + "KB";
        }
        return String.format("%.2f",(double)size/1024/1024) + "M";
    }
}
